/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Messenj;

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 *
 * @author edoardo
 */
public class Player {

    public Player(String type) throws LineUnavailableException, IOException, UnsupportedAudioFileException {
        if (type.equals("beep3")) {

            String path = this.getClass().getResource("/Resources/sound/BINGBING.WAV").getPath();
            File f = new File(path);
            AudioFileFormat aff;
            AudioInputStream ais;



            aff = AudioSystem.getAudioFileFormat(f);

            ais = AudioSystem.getAudioInputStream(f);


            AudioFormat af = aff.getFormat();


            DataLine.Info info = new DataLine.Info(
                    Clip.class,
                    ais.getFormat(),
                    ((int) ais.getFrameLength() *
                    af.getFrameSize()));
            SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
            sdl.open();
            sdl.start();
            int nBytesRead = 0;
            byte[] abData = new byte[58288];

            while (nBytesRead != -1) {
                nBytesRead = ais.read(abData, 0, abData.length);
                if (nBytesRead >= 0) {
                    sdl.write(abData, 0, nBytesRead);
                }

                //Clip ol = (Clip) AudioSystem.getLine(info);
                //ol.open(ais);
                //ol.loop(3);
            }
        }
    }
}






